//
//  RegisterViewController.m
//  Fengly_Nuo_Movie
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 Fengly_Nuo. All rights reserved.
//

#import "RegisterViewController.h"
#import "LTView.h"

// MARK:VIEW_CENTER_X:屏中心x轴
#define VIEW_CENTER_X self.view.center.x

// MARK:VIEW_CENTER_Y:屏中心y轴
#define VIEW_CENTER_Y self.view.center.y

// MARK:HEIGHT:屏高
#define HEIGHT self.view.frame.size.height

// MARK:WIDTH:屏宽
#define WIDTH self.view.frame.size.width

#define kTextField myTextField.text.length

@interface RegisterViewController ()<UITextFieldDelegate>

@property (nonatomic, retain) NSMutableArray *arr;

@end

@implementation RegisterViewController

- (void)dealloc {
    [_arr release];
    Block_release(_accessBlock);
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 页面布局
    [self createRegisterView];
}

// 页面布局
- (void)createRegisterView {
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    imgView.image = [UIImage imageNamed:@"234.jpg"];
    [self.view addSubview:imgView];
    
    UIBarButtonItem *registerButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:(UIBarButtonItemStylePlain) target:self action:@selector(registerButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = registerButtonItem;
    [registerButtonItem release];
    
    NSArray *arrTitle = @[@"用户名：", @"密码：", @"确认密码：", @"邮箱：", @"联系方式："];
    NSArray *arrPrompt = @[@"请输入用户名", @"请输入密码", @"再次输入密码", @"请输入邮箱", @"请输入联系方式"];
    for (NSInteger i = 0; i < 5; i++) {
        LTView *lTView = [[LTView alloc] initWithFrame:CGRectMake(VIEW_CENTER_X - (9 * WIDTH / 10) / 2, VIEW_CENTER_Y / 9 + ((HEIGHT / 14 + 10) * i), 9 * WIDTH / 10, HEIGHT / 14)];
        lTView.myLabel.text = arrTitle[i];
        lTView.tag = 4000 + i;
        lTView.myTextField.placeholder = arrPrompt[i];
        //        lTView.myTextField.clearsOnBeginEditing = YES;
        if (i == 1 || i == 2) {
            lTView.myTextField.secureTextEntry = YES;
        }
        lTView.myTextField.delegate = self;
        [self.view addSubview:lTView];
        [lTView release];
    }
}



#pragma mark - 点击方法
- (void)registerButtonItemAction:(UIBarButtonItem *)buttonItem {
    LTView *view1 = [self.view viewWithTag:4000];
    LTView *view2 = [self.view viewWithTag:4001];
    LTView *view3 = [self.view viewWithTag:4002];
    LTView *view4 = [self.view viewWithTag:4003];
    LTView *view5 = [self.view viewWithTag:4004];
    // 当textField都不为空时
    if (view1.myTextField.text.length && view2.myTextField.text.length && view3.myTextField.text.length && view4.myTextField.text.length && view5.myTextField.text.length) {
        // 当两次密码相同时
        if ([view2.myTextField.text isEqualToString:view3.myTextField.text]) {
            _accessBlock();
            // 当两次密码不同时
        } else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次输入密码不一致" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *OK = [UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyleCancel) handler:nil];
            [alert addAction:OK];
            [self presentViewController:alert animated:YES completion:nil];
        }
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"信息不完整" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *OK = [UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyleCancel) handler:nil];
        [alert addAction:OK];
        [self presentViewController:alert animated:YES completion:nil];
    }
}



#pragma mark - UITextField Delegete
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//    NSLog(@"%ld", textField.tag);
    UITextField *textfield2 = [self.view viewWithTag:textField.tag + 1];
    [textField resignFirstResponder];
    [textfield2 becomeFirstResponder];
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
